import multiprocessing
import threading
import os


class State:
    def __init__(self):

        current_dir = os.path.dirname(__file__)
        data_file_path = os.path.join(current_dir, 'data', 'baselog.txt')
        self.file = open(data_file_path, 'a')
        self.file.write("start\n")
        
        self.x = None
        self.y = None
        self.z = None
        self.pitch = None
        self.roll = None
        self.yaw = None
        self.lon = None
        self.lat = None
        self.time_now = None
        self.u = None
        self.v = None
        self.w = None
        self.p = None
        self.q = None
        self.r = None
        self.du = None
        self.dv = None
        self.dw = None
        self.dp = None
        self.dq = None
        self.dr = None

        self.type = None
        self.No = None

        self.SV_dict = {
            'Head': '$SV',
            'Length': 139,
            'Target-node-type-number': 'U-1',
            'X': 0.0,
            'Y': 0.0,
            'Z': 0.0,
            'pitch': 0.0,
            'roll': 0.0,
            'yaw': 0.0,
            'u': 0.0,
            'v': 0.0,
            'w': 0.0,
            'p': 0.0,
            'q': 0.0,
            'r': 0.0,
            'du': 0.0,
            'dv': 0.0,
            'dw': 0.0,
            'dp': 0.0,
            'dq': 0.0,
            'dr': 0.0,
            'lat': 35.756987,
            'lon': 120.265478,
            'spare1': 1,
            'spare2': 2,
            'spare3': 3,
            'Time': '11:22:33',
            'End': '&&'
        }


    def info_flash(self, data_string):
        if len(data_string) > 20 and data_string[0] == '$':
            try:
                data_list = data_string.split(",")
                self.SV_dict = {
                    'Head': data_list[0],
                    'Length': int(data_list[1]),
                    'Target-node-type-number': data_list[2],
                    'X': float(data_list[3]),
                    'Y': float(data_list[4]),
                    'Z': float(data_list[5]),
                    'pitch': float(data_list[6]),
                    'roll': float(data_list[7]),
                    'yaw': float(data_list[8]),
                    'u': float(data_list[9]),
                    'v': float(data_list[10]),
                    'w': float(data_list[11]),
                    'p': float(data_list[12]),
                    'q': float(data_list[13]),
                    'r': float(data_list[14]),
                    'du': float(data_list[15]),
                    'dv': float(data_list[16]),
                    'dw': float(data_list[17]),
                    'dp': float(data_list[18]),
                    'dq': float(data_list[19]),
                    'dr': float(data_list[20]),
                    'lat': float(data_list[21]),
                    'lon': float(data_list[22]),
                    'spare1': int(data_list[23]),
                    'spare2': int(data_list[24]),
                    'spare3': int(data_list[25]),
                    'Time': data_list[26].split('*')[0],  # 移除 '*&&' 后缀
                    'End': '*&&'
                }
            except Exception as e:
                print(f"error information: {e}")
            self.x = self.SV_dict['X']
            self.y = self.SV_dict['Y']
            self.z = self.SV_dict['Z']
            self.pitch = self.SV_dict['pitch']
            self.roll = self.SV_dict['roll']
            self.yaw = self.SV_dict['yaw']
            self.lon = self.SV_dict['lon']
            self.lat = self.SV_dict['lat']
            self.time_now = self.SV_dict['Time']
            self.u = self.SV_dict['u']
            self.v = self.SV_dict['v']
            self.w = self.SV_dict['w']
            self.p = self.SV_dict['p']
            self.q = self.SV_dict['q']
            self.r = self.SV_dict['r']
            self.du = self.SV_dict['du']
            self.dv = self.SV_dict['dv']
            self.dw = self.SV_dict['dw']
            self.dp = self.SV_dict['dp']
            self.dq = self.SV_dict['dq']
            self.dr = self.SV_dict['dr']

            time_str = "00:00:00"
            self.file.write(data_string + f'\n')



